﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Lists;
using BasicForms;

namespace ReportGenerators
{
    public partial class FrmThamSoBaoCaoCacKhoanNoPhaiThuPhaiTra : BasicForms.FrmThamSoBaoCao
    {
        public FrmThamSoBaoCaoCacKhoanNoPhaiThuPhaiTra()
        {
            InitializeComponent();
        }

        protected override void PrepareDataForReport()
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);

            ReportTemplates.Khac.BaoCaoCacKhoanNoPhaiThuPhaiTra.DsBaoCaoCacKhoanNoPhaiThuPhaiTra ds= new ReportTemplates.Khac.BaoCaoCacKhoanNoPhaiThuPhaiTra.DsBaoCaoCacKhoanNoPhaiThuPhaiTra();
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandTimeout = GeneralLibs.Config.TimeOut;
                SqlDataAdapter da = null;

                cmd.CommandText = "Select_ThongTinDoanhNghiep2";
                cmd.Parameters.Clear();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds.ThongTinDoanhNghiep);

                ds.Master.Rows.Add(ds.Master.NewRow());
                ds.Master.Rows[0]["KyBaoCao"] = string.Format("Tại thời điểm ngày {0}/{1}/{2}", _KyBaoCao.ThoiDiemCuoi.Day, _KyBaoCao.ThoiDiemCuoi.Month, _KyBaoCao.ThoiDiemCuoi.Year); ;
                ds.Master.Rows[0]["ThoiDiemDau"] = _KyBaoCao.ThoiDiemDau;
                ds.Master.Rows[0]["ThoiDiemCuoi"] = _KyBaoCao.ThoiDiemCuoi;

                TaiKhoan taiKhoan131 = DanhMucTaiKhoan.Instance.GetBySoHieu("131");
                TaiKhoan taiKhoan331 = DanhMucTaiKhoan.Instance.GetBySoHieu("331");

                foreach (DoiTuongNghiepVu doiTac in DanhMucDoiTuongNghiepVu.GetDanhMucDoiTac())
                {
                    DataRow reportRow = ds.Detail.NewRow();
                    reportRow["SoHieuVaTenTaiKhoan"] = string.Format("TK{0} - {1}", taiKhoan131.SoHieu, taiKhoan131.Ten);
                    reportRow["TenDoiTac"] = doiTac.Ten;

                    SoDuTaiKhoan soDu = SoDuTaiKhoan.CalculateFromDB(cmd, _KyBaoCao.ThoiDiemCuoi, taiKhoan131.ID, doiTac.ID);
                    if (soDu.DuNo > 0)
                    {
                        reportRow["PhaiThuHayPhaiTra"] = string.Format("I.         Các khoản phải thu (Nợ TK{0})", taiKhoan131.SoHieu);
                        reportRow["SoDu"] = soDu.DuNo;
                    }
                    else if (soDu.DuCo > 0)
                    {
                        reportRow["PhaiThuHayPhaiTra"] = string.Format("II.         Các khoản phải trả (Có TK{0})", taiKhoan131.SoHieu);
                        reportRow["SoDu"] = soDu.DuCo;
                    }
                    else
                    {
                        continue;
                    }
                    ds.Detail.Rows.Add(reportRow);
                }

                foreach (DoiTuongNghiepVu doiTac in DanhMucDoiTuongNghiepVu.GetDanhMucDoiTac())
                {
                    DataRow reportRow = ds.Detail.NewRow();
                    reportRow["SoHieuVaTenTaiKhoan"] = string.Format("TK{0} - {1}", taiKhoan331.SoHieu, taiKhoan331.Ten);
                    reportRow["TenDoiTac"] = doiTac.Ten;

                    SoDuTaiKhoan soDu = SoDuTaiKhoan.CalculateFromDB(cmd, _KyBaoCao.ThoiDiemCuoi, taiKhoan331.ID, doiTac.ID);
                    if (soDu.DuNo > 0)
                    {
                        reportRow["PhaiThuHayPhaiTra"] = string.Format("I.         Các khoản phải thu (Nợ TK{0})", taiKhoan331.SoHieu);
                        reportRow["SoDu"] = soDu.DuNo;
                    }
                    else if (soDu.DuCo > 0)
                    {
                        reportRow["PhaiThuHayPhaiTra"] = string.Format("II.         Các khoản phải trả (Có TK{0})", taiKhoan331.SoHieu);
                        reportRow["SoDu"] = soDu.DuCo;
                    }
                    else
                    {
                        continue;
                    }
                    ds.Detail.Rows.Add(reportRow);
                }
              
                if (ds.Tables.Count > 0)
                {
                    _Report = new ReportTemplates.Khac.BaoCaoCacKhoanNoPhaiThuPhaiTra.RptBaoCaoCacKhoanNoPhaiThuPhaiTra();
                    _Report.SetDataSource(ds);
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
                con = null;
            }
        }

        protected override void PrintReport()
        {
            if (_Report != null)
            {
                ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(_Report, "Báo cáo công nợ phải thu, phải trả");
                frm.Show();
            }
        }
    }
}
